//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Globalization;
using OpenLS.Core.Serialization;

namespace OpenLS.Spreadsheet
{
    class ExternalCell: IOfficeXmlSerializable
    {
        #region IOfficeXmlSerializable Members

        public void ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("cell"))
            {
                Ref = c.GetOptionalString("r");
                Type = c.GetOptionalEnum<CellType>("t");
                ValueMetadata = c.GetOptionalUInteger("vm", 0);
                RawValue = c.ReadOptionalElementString("v");
            }
        }

        public void WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("cell"))
            {
                c.SetOptionalString("r", Ref);
                c.SetOptionalEnum("t", Type);
                c.SetOptionalUInteger("vm", ValueMetadata, 0);
                c.WriteOptionalElementString("v", RawValue);
            }
        }

        #endregion

        private string _v;

        public string RawValue
        {
            get { return _v; }
            set { _v = value; }
        }
        public object Value
        {
            get
            {
                if (Type == null)
                    return RawValue;
                switch (Type.Value)
                {
                    case CellType.Boolean:
                        return Cell.GetBooleanValue(RawValue);
                    case CellType.Error:
                        return Error.Parse(RawValue);
                    case CellType.FormulaString:
                        return RawValue;
                    case CellType.InlineString:
                        return RawValue;
                    case CellType.Number:
                        return double.Parse(RawValue, CultureInfo.InvariantCulture);
                    case CellType.SharedString:
                        throw new NotImplementedException();
                    default: throw new NotSupportedException();
                }
            }
        }
        private string _r;

        public string Ref
        {
            get { return _r; }
            set { _r = value; }
        }
        private CellType? _t;

        public CellType? Type
        {
            get { return _t; }
            set { _t = value; }
        }
        private uint _vm;

        public uint ValueMetadata
        {
            get { return _vm; }
            set { _vm = value; }
        }
	
	
	
    }
}
